package core
{
	import com.brokenfunction.json.decodeJson;
	import com.brokenfunction.json.encodeJson;
	
	import flash.system.Security;
	
	import org.aisy.display.USprite;
	import org.aisy.utils.AisyUtils;
	
	import taylor.net.DataLoad;

	public class Main extends USprite
	{
		public function Main()
		{
			try {
				Security.allowDomain("*");
				Security.allowInsecureDomain("*");
			}
			catch (error:*) {}
		}
		
		public function connect(im:*):void
		{
			var url:String = loaderInfo.parameters["url"];
			if (url) DataLoad.load(parseData, url);
			url = null;
			im = null;
		}
		
		public function parseData(data:Object):void
		{
			if (data.hasOwnProperty("vars") === true) {
				var vars:Object = data.vars;
				var vars2:Object = AisyUtils.cloneObjectByByteArray(vars);
				for (var i:String in vars) {
					if (loaderInfo.parameters.hasOwnProperty(i) === true) vars2[i] = loaderInfo.parameters[i];
				}
				vars = vars2;
				var varStr:String = encodeJson(vars);
				var r:RegExp = /\$\{([^\{\}]+)\}/g;
				for (i in vars) {
					varStr = varStr.replace(r, function ():String
					{
						if (i === arguments[1]) return vars[i];
						return arguments[0];
					});
					vars = decodeJson(varStr);
				}
				data.vars = vars;
				var str:String = encodeJson(data);
				str = str.replace(r, function ():String
				{
					if (vars.hasOwnProperty(arguments[1]) === true) return vars[arguments[1]];
					return arguments[0];
				});
				data = decodeJson(str);
				str = null;
				varStr = null;
				vars2 = null;
				vars = null;
				r = null;
			}
			var l:Loading = new Loading();
			l.load(data, this);
			l = null;
			data = null;
		}
		
	}
}